package net.wenku8.reader.view.file

import android.util.Log
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.viewModelScope
import com.alibaba.fastjson2.JSON
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import net.wenku8.reader.entity.BookFile
import net.wenku8.reader.entity.BookFileChapter
import net.wenku8.reader.enums.FileBookChapter
import net.wenku8.reader.enums.FileBookInfo
import net.wenku8.reader.enums.FileBookText
import net.wenku8.reader.service.FileService
import net.wenku8.reader.state.StateReadFile
import net.wenku8.reader.view.common.BaseViewModel

class FileDetailViewModel : BaseViewModel() {
    var bookFile by mutableStateOf(BookFile())
    val chapterList = mutableStateListOf<BookFileChapter>()
    var loaded by mutableStateOf(false)

    fun initData() {
        viewModelScope.launch(Dispatchers.IO) {
            loaded = false
            val bookId = StateReadFile.bookFile.value.bookId
            Log.i("initData", "bookId:$bookId")
            val chapterString = FileService.readAsString(FileBookChapter+bookId)
            if (chapterString != null && StateReadFile.bookFile.value.chapterSize > 0){
                val list = JSON.parseArray(chapterString, BookFileChapter::class.java)
                chapterList.clear()
                chapterList.addAll(list)
            }else{
                val lineList = FileService.readAsLine(FileBookText +bookId)
                val list = lineList?.let { getChapterList(it) }
                if (list == null) {
                    return@launch
                }
                StateReadFile.bookFile.value.textSize = lineList.stream().mapToInt(String::length).sum()
                StateReadFile.bookFile.value.chapterSize = list.size
                FileService.writeFile(FileBookInfo+bookId, JSON.toJSONString(StateReadFile.bookFile.value))
                FileService.writeFile(FileBookChapter+bookId, JSON.toJSONString(list))
                chapterList.clear()
                chapterList.addAll(list)
            }
            bookFile = StateReadFile.bookFile.value
            loaded = true
        }
    }

    private fun getChapterList(lineList : List<String>):ArrayList<BookFileChapter>{
        val list = ArrayList<BookFileChapter>()
        val regex = Regex("^.*第[0-9一二三四五六七八九十百千万亿]{0,10}[章卷].*")
        var chapterIndex = 0
        var chapterLineStart = 0
        for( line in lineList){
            chapterLineStart++
            val lineStr = line.trim()
            if (regex.matches(lineStr)){
                if(list.isEmpty() || list.last().title != lineStr && list.last().chapterLineStart != chapterLineStart-1){
                    if(list.size > 0){
                        list.last().chapterLineEnd = chapterLineStart - 1
                    }
                    list.add(BookFileChapter(lineStr, chapterIndex, chapterLineStart))
                    chapterIndex++
                }
            }else{
                if (list.isEmpty()){
                    list.add(BookFileChapter("开始", 0, 0))
                    chapterIndex++
                }
            }
        }
        return list
    }

    fun toFileRead(chapter : BookFileChapter?){
        if (chapter == null){
            return
        }
        StateReadFile.chapter.value = chapter
        StateReadFile.chapterList.value = ArrayList(chapterList)
    }
}